// malred-vue-template-js/src/utils/menu.js
/*
 * @Author Malred
 * @Date 2025-06-01 22:26:39
 * @Description 菜单工具类
 */

export function buildMenuTree(routes) {
    console.log('build menu routes', routes)
    const tree = [];
    const map = {};

    // 先全部映射到 map
    routes.forEach(route => {
        map[route.path] = { ...route, children: [] };
    });

    // 构建树结构
    routes.forEach(route => {
        const path = route.path;
        const parentPath = path.substring(0, path.lastIndexOf('/'));

        if (parentPath && map[parentPath]) {
            map[parentPath].children.push(map[path]);
        } else {
            tree.push(map[path]);
        }
    });

    return tree;
}
